<?php
  
  //Main 
  $keys = array("country", "filter","key");
  $value = array(); 
  foreach( $keys as $key ) {
    $value[$key] = $_GET[$key]; 
  }
  ///////////////////////////////////////////
  //Switch check prameter
  //URL : http://localhost/govss/php/index.php?country=TH&filter=xxx
  ///////////////////////////////////////////
  switch (true) {
      case ($value['country'] && $value['filter']) : 

          //Switch filter 
          switch ($value['filter']) {
              case "province": 
                  //Loop get parameter
                  $keys = array("proc", "type","data");
                  $map = array(); 
                  foreach( $keys as $key ) {
                    $map[$key] = $_GET[$key]; 
                  }

                  //Check paramter for display map
                  switch (true) {
                      ///////////////////////////////////////////
                      //Get province map
                      //URL : http://localhost/govss/php/index.php?country=TH&filter=province&type=map&proc=84&data=ratio
                      ///////////////////////////////////////////
                      case  ($map["type"]=="map") :
                            include("ProvinceMap.class.php");
                            //Switch check type of map
                            switch ($map["data"]) {
                                case "ratio":
                                    ProvinceMap::getPovertyRatioMapByCode($map["proc"]);
                                    break;
                                case "gap":
                                    ProvinceMap::getPovertyGapMapByCode($map["proc"]);
                                    break;
                                case "violence":
                                    ProvinceMap::getPovertyViolenceMapByCode($map["proc"]);
                                    break;
                                default:
                                    ProvinceMap::getDefaultMapByCode($map["proc"]);
                                    break;
                            } //End switch : check $map['data']
                            break; //End case : check $map["type"]=="map" && isset($map["code"]) && isset($map["data"]
                      ///////////////////////////////////////////
                      //Get province chart
                      //URL : http://localhost/govss/php/index.php?country=TH&filter=province&type=map&proc=84&data=ratio
                      ///////////////////////////////////////////
                      case  ($map["type"]=="chart") :
                            include("ProvinceChart.class.php");
                            //Switch check type of map
                            switch ($map["data"]) {
                                case "ratio":
                                    ProvinceChart::getPovertyRatioChartByCode($map["proc"]);
                                    break;
                                case "gap":
                                    ProvinceChart::getPovertyGapChartByCode($map["proc"]);
                                    break;
                                case "violence":
                                    ProvinceChart::getPovertyViolenceChartByCode($map["proc"]);
                                    break;
                                default:
                                    ProvinceChart::getPovertyDefaultChartByCode($map["proc"]);
                                    break;
                            } //End switch : check $map['data']
                            break; //End case : check $map["type"]=="map" && isset($map["code"]) && isset($map["data"]
                      default:
                            ///////////////////////////////////////////
                            //Get province XML
                            //URL : http://localhost/govss/php/index.php?country=TH&filter=province
                            ///////////////////////////////////////////
                            include("Province.class.php");
                            $vote = new Province();

                            if($value['key'] && strlen($value['key'])>0){
                              $vote->searchProvince($value);
                            }else{
                              $vote->getProvince($value);
                            }
                            break;
                  } //End switch : check $value['filter']
                  break; //End case : check $value['country'] && $value['filter']

              case "district":
                  //Loop get parameter
                  $keys = array("proc", "type","data");
                  $map = array(); 
                  foreach( $keys as $key ) {
                    $map[$key] = $_GET[$key]; 
                  }

                  //Check paramter for display map
                  switch (true) {
                      ///////////////////////////////////////////
                      //Get district map
                      //URL : http://localhost/govss/php/index.php?country=TH&filter=province&type=map&proc=84&data=ratio
                      ///////////////////////////////////////////
                      case  ($map["type"]=="map") :
                            include("DistrictMap.class.php");
                            //Switch check type of map
                            switch ($map["data"]) {
                                case "ratio":
                                    DistrictMap::getPovertyRatioMapByCode($map["proc"]);
                                    break;
                                case "gap":
                                    DistrictMap::getPovertyGapMapByCode($map["proc"]);
                                    break;
                                case "violence":
                                    DistrictMap::getPovertyViolenceMapByCode($map["proc"]);
                                    break;
                                default:   
                                    DistrictMap::getDefaultMapByCode($map["proc"]);
                                    break;
                            } //End switch : check $map['data']
                            break; //End case : check $map["type"]=="map" && isset($map["code"]) && isset($map["data"]
                      ///////////////////////////////////////////
                      //Get district chart
                      //URL : http://localhost/govss/php/index.php?country=TH&filter=province&type=map&proc=84&data=ratio
                      ///////////////////////////////////////////
                      case  ($map["type"]=="chart") :
                            include("DistrictChart.class.php");
                            //Switch check type of map
                            switch ($map["data"]) {
                                case "ratio":
                                    DistrictChart::getPovertyRatioChartByCode($map["proc"]);
                                    break;
                                case "gap":
                                    DistrictChart::getPovertyGapChartByCode($map["proc"]);
                                    break;
                                case "violence":
                                    DistrictChart::getPovertyViolenceChartByCode($map["proc"]);
                                    break;
                                default:
                                    DistrictChart::getDefaultMapByCode($map["proc"]);
                                    break;
                            } //End switch : check $map['data']
                            break; //End case : check $map["type"]=="map" && isset($map["code"]) && isset($map["data"]
                      default:
                            include("District.class.php");
                            $vote = new District();

                            if($value['key'] && strlen($value['key'])>0){
                              $vote->searchDistrict($value);
                            }else{
                              $value['proc'] = $_GET['proc'];
                              $vote->getDistrict($value);
                            }
                            unset($vote);
                            break;
                  } //End switch : check $value['filter']
                  break;

              case "tambon":
                  //echo "i equals 2";
                  //Get Tambon
                  $keys = array("proc", "disc", "type", "data");
                  $map = array(); 
                  foreach( $keys as $key ) {
                    $map[$key] = $_GET[$key]; 
                  }

                  //Check paramter for display map
                  switch (true) {
                      ///////////////////////////////////////////
                      //Get district map
                      //URL : http://localhost/govss/php/index.php?country=TH&filter=province&type=map&proc=84&data=ratio
                      ///////////////////////////////////////////
                      case  ($map["type"]=="map") :
                            include("TambonMap.class.php");
                            //Switch check type of map
                            switch ($map["data"]) {
                                case "ratio":
                                    TambonMap::getPovertyRatioMapByCode($map["proc"],$map["disc"]);
                                    break;
                                case "gap":
                                    TambonMap::getPovertyGapMapByCode($map["proc"],$map["disc"]);
                                    break;
                                case "violence":
                                    TambonMap::getPovertyViolenceMapByCode($map["proc"],$map["disc"]);
                                    break;
                                case "remark":
                                    TambonMap::getRemarkMapByCode($map["proc"],$map["disc"]);
                                    break;
                                default:
                                    TambonMap::getDefaultMapByCode($map["proc"],$map["disc"]);
                                    break;
                            } //End switch : check $map['data']
                            break; //End case : check $map["type"]=="map" && isset($map["code"]) && isset($map["data"]
                      ///////////////////////////////////////////
                      //Get district chart
                      //URL : http://localhost/govss/php/index.php?country=TH&filter=province&type=map&proc=84&data=ratio
                      ///////////////////////////////////////////
                      case  ($map["type"]=="chart") :
                            include("TambonChart.class.php");
                            //Switch check type of map
                            switch ($map["data"]) {
                                case "ratio":
                                    TambonChart::getPovertyRatioChartByCode($map["proc"],$map["disc"]);
                                    break;
                                case "gap":
                                    TambonChart::getPovertyGapChartByCode($map["proc"],$map["disc"]);
                                    break;
                                case "violence":
                                    TambonChart::getPovertyViolenceChartByCode($map["proc"],$map["disc"]);
                                    break;
                                case "remark":
                                    TambonChart::getRemarkChartByCode($map["proc"],$map["disc"]);
                                    break;
                                default:
                                    TambonChart::getPovertyDefaultChartByCode($map["proc"],$map["disc"]);
                                    break;
                            } //End switch : check $map['data']
                            break; //End case : check $map["type"]=="map" && isset($map["code"]) && isset($map["data"]
                      default:
                            include("Tambon.class.php");
                            $vote = new Tambon();
                            if($value['key'] && strlen($value['key'])>0){
                              $vote->searchTambon($value);

                              

                            }else{

                              $value['proc'] = $_GET['proc']; 
                              $value['disc'] = $_GET['disc']; 
                              $vote->getTambon($value);
                              
                            }
                            unset($vote);
                            
                            break;
                  } //End switch : check $value['filter']

                  break;

              case "moo":
                  //Get Moo
                  $value['pro'] = $_GET['pro']; 
                  $value['dis'] = $_GET['dis']; 
                  $value['tam'] = $_GET['tam']; 
                  include("Moo.class.php");
                  $vote = new Moo();
                  $vote->getMoo($value);
                  unset($vote);
                  break;

              case "house_no":
                  //Get HouseNo
                  include("HouseNo.class.php");
                  $vote = new HouseNo();
                  $value['pro'] = $_GET['pro']; 
                  $value['dis'] = $_GET['dis']; 
                  $value['tam'] = $_GET['tam']; 
                  $value['moo'] = $_GET['moo']; 

                  $vote->getHouseNo($value);
                  unset($vote);
                  break;

              case "person":
                  //Get Person
                  include("Person.class.php");
                  $vote = new Person();

                  $value['first'] = $_GET['first']; 
                  $value['last'] = $_GET['last']; 

                  if($value['first'] && strlen(trim($value['first']))>0 && $value['last'] && strlen(trim($value['last']))>0 ){
                    $vote->searchPerson($value);
                  }else if($value['first'] && strlen(trim($value['first']))>0 ){
                    $vote->searchFirstName($value);
                  }else if($value['last'] && strlen(trim($value['last']))>0 ){
                    $vote->searchLastName($value);
                  }else{
                    $value['pro'] = $_GET['pro']; 
                    $value['dis'] = $_GET['dis']; 
                    $value['tam'] = $_GET['tam']; 
                    $value['moo'] = $_GET['moo']; 
                    $value['hou'] = $_GET['hou']; 
                    $vote->getPerson($value);
                  }
                  unset($vote);
                  break;

              default:
                  $response['errorCode'] = 6;
                  $response['errorString'] = "Wrong parameter filter.";
                  setErrorResponse($response);
                  break;

          } //End switch : check $value['filter']
          break; //End case : check $value['country'] && $value['filter']

      default:
          $response['errorCode'] = 7;
          $response['errorString'] = "Parameter incomplete.";
          setErrorResponse($response);
          break;

  } //End switch : check prameter



/*
  if($value['country'] && $value['filter']){
    if($value['filter']=="province"){

      $keys = array("code", "type","data");
      $map = array(); 
      foreach( $keys as $key ) {
        $map[$key] = $_GET[$key]; 
      }

      if($map["type"]=="map" && isset($map["code"]) && isset($map["data"])){
        include("ProvinceMap.class.php");

        if($map["data"] == "ratio"){
          ProvinceMap::getPovertyRatioMapByCode($code);
        }else if($map["data"] == "gap"){
          ProvinceMap::getPovertyGapMapByCode($code);
        }else if($map["data"] == "violence"){
          ProvinceMap::getPovertyViolenceMapByCode($code);
        }


      }else{
        //Get province
        include("Province.class.php");
        $vote = new Province();

        if($value['key'] && strlen($value['key'])>0){
          $vote->searchProvince($value);
        }else{
          $vote->getProvince($value);
        }
      }
      unset($vote);
    }else if($value['filter']=="district"){
      //Get District
      include("District.class.php");
      $vote = new District();

      if($value['key'] && strlen($value['key'])>0){
        $vote->searchDistrict($value);
      }else{
        $value['pro'] = $_GET['pro']; 
        $vote->getDistrict($value);
      }
      unset($vote);
    }else if($value['filter']=="tambon"){
      //Get Tambon
      include("Tambon.class.php");
      $vote = new Tambon();

      if($value['key'] && strlen($value['key'])>0){
        $vote->searchTambon($value);
      }else{

        $value['pro'] = $_GET['pro']; 
        $value['dis'] = $_GET['dis']; 
        $vote->getTambon($value);
      }
      unset($vote);
    }else if($value['filter']=="moo"){
      //Get Moo
      $value['pro'] = $_GET['pro']; 
      $value['dis'] = $_GET['dis']; 
      $value['tam'] = $_GET['tam']; 
      include("Moo.class.php");
      $vote = new Moo();
      $vote->getMoo($value);
      unset($vote);
    }else if($value['filter']=="house_no"){
      //Get HouseNo
      include("HouseNo.class.php");
      $vote = new HouseNo();
      $value['pro'] = $_GET['pro']; 
      $value['dis'] = $_GET['dis']; 
      $value['tam'] = $_GET['tam']; 
      $value['moo'] = $_GET['moo']; 

      $vote->getHouseNo($value);
      unset($vote);
    }else if($value['filter']=="person"){
      //Get Person
      include("Person.class.php");
      $vote = new Person();

      $value['first'] = $_GET['first']; 
      $value['last'] = $_GET['last']; 

      if($value['first'] && strlen(trim($value['first']))>0 && $value['last'] && strlen(trim($value['last']))>0 ){
        $vote->searchPerson($value);
      }else if($value['first'] && strlen(trim($value['first']))>0 ){
        $vote->searchFirstName($value);
      }else if($value['last'] && strlen(trim($value['last']))>0 ){
        $vote->searchLastName($value);
      }else{
        $value['pro'] = $_GET['pro']; 
        $value['dis'] = $_GET['dis']; 
        $value['tam'] = $_GET['tam']; 
        $value['moo'] = $_GET['moo']; 
        $value['hou'] = $_GET['hou']; 
        $vote->getPerson($value);
      }
      unset($vote);
    }else {
      $response['errorCode'] = 6;
      $response['errorString'] = "Wrong parameter filter.";
      setErrorResponse($response);
    }
  }else{
      $response['errorCode'] = 7;
      $response['errorString'] = "Parameter incomplete.";
      setErrorResponse($response);
  }
*/
  function setErrorResponse($response){
        //Gen food
        $xml = new DOMDocument('1.0', 'UTF-8');
        $xml->formatOutput = true;

        $xml_node_response = $xml->createElement("response");
        $xml->appendChild( $xml_node_response );
        $xml_node_errorcode = $xml->createElement( "errorcode", $response['errorCode']);
        $xml_node_response->appendChild( $xml_node_errorcode );
        $xml_node_errorstring = $xml->createElement( "errorstring", $response['errorString']);
        $xml_node_response->appendChild( $xml_node_errorstring );

        echo $xml->saveXML();
  }
?>

